unit displayfilter; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Buttons, Sqlite3DS;

type

  { TProductFilterForm }

  TProductFilterForm = class ( TForm )
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Filter1List: TListBox;
    Filter2List: TListBox;
    Filter3List: TListBox;
    ButtonPanel: TPanel;
    procedure Filter1ListSelectionChange(Sender: TObject; User: boolean);
    procedure Filter2ListSelectionChange(Sender: TObject; User: boolean);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    procedure SetFilter;
  public
    { public declarations }
    FilterText: string;
    FilterSQL: string;
  end; 

var
  ProductFilterForm: TProductFilterForm;

implementation

uses main, fpstructs;

{ TProductFilterForm }

procedure TProductFilterForm.Filter1ListSelectionChange(Sender: TObject;
  User: boolean);
var
  r: integer;
  ix : integer;
  x: Integer;
begin
  ix := TListBox(Sender).ItemIndex;
  if ix=-1 then ix := 0;
  Filter2List.Clear;
  Filter3List.Clear;
  Case ix of
       1: {for x := 0 to CLASSMAX do
              Filter2List.Items.Add(ProductClasses[x])};
       2: try
            MainDB.Close;
            MainDB.SQL := 'SELECT id, name FROM suppliers ORDER BY name';
            MainDB.Open;
            while not MainDB.EOF do
              begin
                Filter2List.Items.AddObject(MainDB.FieldByName('name').AsString,
                                       TObject(MainDB.FieldByName('id').AsInteger));
                MainDB.Next;
              end;
              Filter3List.Items.Add('All');
              {for x := 0 to CLASSMAX do
                  Filter3List.Items.Add(ProductClasses[x]); }
            finally
            MainDB.Close;
          end;
  end;
  if Filter2List. Count > 0
     then Filter2List.ItemIndex := 0;
  if Filter3List.Items.Count > 0
     then Filter3List.ItemIndex := 0;
  SetFilter;
end;

procedure TProductFilterForm.Filter2ListSelectionChange(Sender: TObject;
  User: boolean);
var
  ix: LongInt;
begin
  ix := TListBox(Sender).ItemIndex;
  if TListBox(Sender).Count > 0
     then if ix=-1 then ix := 0;
  SetFilter;
end;

procedure TProductFilterForm.FormCreate(Sender: TObject);
begin
  FilterSQL := '';
  FilterText := 'All';
end;

procedure TProductFilterForm.SetFilter;
begin
  FilterText := Filter1List.Items[Filter1List.ItemIndex];
  if Filter2List.ItemIndex > -1
     then FilterText := FilterText + '|' + Filter2List.Items[Filter2List.ItemIndex];
  if Filter3List.ItemIndex > -1 then FilterText := FilterText + '|' + Filter3List.Items[Filter3List.ItemIndex];
  If Filter1List.ItemIndex < 0 then Filter1List.ItemIndex := 0;
  case Filter1List.ItemIndex of
       0: FilterSQL := '';
       1: FilterSQL := ' AND products.class=' + IntToStr(Filter2List.ItemIndex);
       2: begin
            FilterSQL := ' AND suppliers.name=''' + Filter2List.Items[Filter2List.ItemIndex] + '''';
            if Filter3List.ItemIndex > 0
               then FilterSQL := FilterSQL + ' AND products.class=' + IntToStr(Filter3List.ItemIndex);
          end;
       end;
end;

initialization
  {$I displayfilter.lrs}

end.

